
/* -----------------
* brief 
* 1. this is code gen by tools 
* 2. for add/update/delete/query the data save in sql database   
*/

package tenant

import (
	"errors"
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"saas_sys_base/redishelper"

	//"github.com/mjiulee/lego/utils"
)
const (
	Field_RegisterState = "register_state"
	Field_IsPay = "is_payed"

)


const REDIS_TENAT_BY_ID_PREFIX string = "tenant_by_id:"

func init() {
	//CheckRedisKey(REDIS_TENAT_BY_ID_PREFIX) //检查是否有重复key
	//CheckRedisKey(REDIS_TENAT_BY_WX_ID_PREFIX)
}

/*
实验性代码
*/
func (t *Tenant) CacheUpdateAllBy(item *Tenant) {
	if item.Id <= 0 {
		lego.LogError(errors.New("item.Id <= 0 in CacheUpdateAllBy"))
		return
	}
	_ = redishelper.RedisSetKeyEx(REDIS_TENAT_BY_ID_PREFIX+utils.Int64ToString(item.Id), item)
}

func (t *Tenant) CacheDelAllBy(item *Tenant) {
	if item.Id <= 0 {
		lego.LogError(errors.New("item.Id <= 0 in CacheDelAllBy"))
		return
	}
	_ = redishelper.RedisDelKey(REDIS_TENAT_BY_ID_PREFIX+utils.Int64ToString(item.Id))
}
/* 根据id获取记录
 * -----------------
 */
func (t *Tenant) RecodeById(id int64) *Tenant {
	item := new(Tenant)
	err := redishelper.RedisGetKeyEx(REDIS_TENAT_BY_ID_PREFIX+utils.Int64ToString(id), item)
	if err == nil {
		return item
	}

	_, err = lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}

	t.CacheUpdateAllBy(item)
	return item
}

/* 添加
 * -----------------
 */
func (t *Tenant) AddRecode(item2add *Tenant) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}

	t.CacheUpdateAllBy(item2add)
	return true
}

func (t *Tenant) AddRecodeWithsession(session*xorm.Session,item2add *Tenant) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}

	t.CacheUpdateAllBy(item2add)
	return true
}

/* 删除(硬删除)*/
func (t *Tenant) DelRecodeById(id int64) bool {
	item := new(Tenant)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
        lego.LogError(err)
        return false
    }

	itemDel := new(Tenant)
	_ = redishelper.RedisGetKeyEx(REDIS_TENAT_BY_ID_PREFIX+utils.Int64ToString(id), itemDel)
	t.CacheDelAllBy(itemDel)

    return true
}

func (t *Tenant) DelRecodeByIdWithSession(session*xorm.Session,id int64) bool {
	item := new(Tenant)
	_, err := session.ID(id).Delete(item)
	if err != nil {
        lego.LogError(err)
        return false
    }

	itemDel := new(Tenant)
	_ = redishelper.RedisGetKeyEx(REDIS_TENAT_BY_ID_PREFIX+utils.Int64ToString(id), itemDel)
	t.CacheDelAllBy(itemDel)

    return true
}

/* 更新
 * -----------------*/
func (t *Tenant) UpdateRecode(rc *Tenant) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Update(rc)
	if err != nil {
        lego.LogError(err)
        return false
    }

	t.CacheDelAllBy(rc)
    return true
}


func (t *Tenant) UpdateRecodeFields(rc *Tenant, cols ...string) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Cols(cols...).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}

	t.CacheDelAllBy(rc)
	return true
}

func (t *Tenant) UpdateRecodeWithSession(session *xorm.Session,rc *Tenant) bool {
	_, err := session.ID(rc.Id).Update(rc)
	if err != nil {
        lego.LogError(err)
        return false
    }

	t.CacheDelAllBy(rc)
    return true
}


/*
func TestDengjin() {
	tableinfo := lego.GetDBEngine().TableInfo(Tenant{})

	name := tableinfo.Name
	name = name
    fieldlist := tableinfo.Columns()
	fieldlist = fieldlist

}*/